package logic

import (
	"api/model/order_food_category"
	"api/service"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlx"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderFoodCategoryUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewOrderFoodCategoryUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderFoodCategoryUpdateLogic {
	return &OrderFoodCategoryUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *OrderFoodCategoryUpdateLogic) OrderFoodCategoryUpdate(req *types.RequestOrderFoodCategoryUpdate) (resp *types.ResponseMsg, err error) {
	conn := sqlx.NewMysql(l.svcCtx.Config.DataSource)
	categoryModel := order_food_category.NewOrderFoodCategoryModel(conn)

	busService := service.NewBusinessService(l.ctx, l.svcCtx)
	bus := busService.GetLoginBusinessInfo()

	err = categoryModel.Update(l.ctx, &order_food_category.OrderFoodCategory{
		Id:         req.Id,
		Title:      req.Title,
		BusinessId: bus.Id,
	})

	if err != nil {
		fmt.Println(err.Error())
		return service.ReturnError()
	}

	return service.ReturnOk()
}
